﻿using System;
using System.Deployment;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;

namespace Netfraction.Common.Logging
{
    public class LogManager
    {
        private static LogLevel _currentLoggingLevel = LogLevel.Basic;
        private static TextDataStore _logStore;

        private static bool CheckStoreExists()
        {
            _logStore = new TextDataStore(LogDataSource.Source, string.Format("Log - {0}.txt", DateTime.Now.ToString("yyyy-MM")));
            return _logStore != null;
        }

        public static void WriteEntry(string message, LogLevel level)
        {
            WriteEntry(message, LogEntryType.Information, LogEventId.Generic, level);
        }

        public static void WriteEntry(string message, LogEventId eventId, LogLevel level)
        {
            WriteEntry(message, LogEntryType.Information, eventId, level);
        }

        public static void WriteEntry(string message, LogEntryType entryType, LogEventId eventId, LogLevel level)
        {
            try
            {
                if ((int)level >= (int)_currentLoggingLevel)
                {
                    if (!CheckStoreExists())
                        return;

                    using (StreamWriter sWriter = _logStore.CreateWriter())
                    {
                        sWriter.WriteLine("{0}|{1}|{2}|{3}|{4}", DateTime.Now.ToString("s"), message, entryType, eventId, GetEventCategory((int)eventId));
                    }
                }
            }
            catch (ArgumentException)
            {
            }
            catch (Exception)
            {
            }
        }

        private static LogEventCategory GetEventCategory(int eventId)
        {
            if (eventId >= 100 && eventId <= 199)
                return LogEventCategory.General;
            else if (eventId >= 200 && eventId <= 299)
                return LogEventCategory.Protocol;
            else if (eventId >= 300 && eventId <= 399)
                return LogEventCategory.Security;
            else if (eventId >= 400 && eventId <= 499)
                return LogEventCategory.File;
            else if (eventId >= 500 && eventId <= 599)
                return LogEventCategory.User;
            else
                return LogEventCategory.General;
        }

        private static EventLogEntryType GetLogEntryType(LogEntryType entryType)
        {
            switch (entryType)
            {
                case LogEntryType.Information:
                    return EventLogEntryType.Information;
                case LogEntryType.Warning:
                    return EventLogEntryType.Warning;
                case LogEntryType.Error:
                    return EventLogEntryType.Error;
                case LogEntryType.SecuritySuccess:
                    return EventLogEntryType.SuccessAudit;
                case LogEntryType.SecurityFailure:
                    return EventLogEntryType.FailureAudit;
                default:
                    return EventLogEntryType.Information;
            }
        }
    }
}
